package com.sunlg.smarthome.ui.environment;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.EnvironmentContract;
import com.sunlg.smarthome.contract.presenter.EnvironmentPresenter;
import com.sunlg.smarthome.data.entity.TempAndHum;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TempHumListActivity extends BaseActivity implements EnvironmentContract.View{
    @BindView(R.id.rv_temp_hum_list)
    RecyclerView rvTempHum;

    private List<TempAndHum> tempAndHumList;
    private TempAndHumAdapter envirModelAdapter;
    private LinearLayoutManager layoutManager;
    private EnvironmentContract.Presenter presenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_temp_hum_list;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("环境监测");
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        layoutManager = new LinearLayoutManager(this);
        tempAndHumList = new ArrayList<>();
        envirModelAdapter = new TempAndHumAdapter(this, tempAndHumList);

        presenter = new EnvironmentPresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initView() {
        rvTempHum.setLayoutManager(layoutManager);
        rvTempHum.setAdapter(envirModelAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_envirdevice, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_envir_device){
            overlay(EnvirDeviceListActivity.class);
        }else if(item.getItemId() == R.id.action_air_conditioner){
            overlay(AirConditionerActivity.class);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void loadAllTemAndHumSuccess(List<TempAndHum> tempAndHumList) {
        this.tempAndHumList.clear();
        this.tempAndHumList.addAll(tempAndHumList);
        this.envirModelAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.loadAllTempAndHum();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
    }
}
